﻿using UnityEditor;
using UnityEngine;
using WentStateMachine;

public class AnimatorDriver : MonoBehaviour,IDriver
{
    private Animator animator;

    public IStateMachineController Drive(StateMachineController asset)
    {
        checkAnimator();
        var path = AssetDatabase.GUIDToAssetPath(asset.AnimatorControllerGUID);
        RuntimeAnimatorController controller = AssetDatabase.LoadAssetAtPath<RuntimeAnimatorController>(path);
        animator.runtimeAnimatorController = controller;
        return new ControllerForAnimator(animator);
    }

    void Start()
    {
        checkAnimator();
    }

    private void checkAnimator()
    {
        if (!animator)
            animator = GetComponent<Animator>();
        else
            return;
        if (!animator)
        {
            animator = gameObject.AddComponent<Animator>();
        }
    }
}
